﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using AutoCodeCSharp.Models;
using AutoCodeCSharp.DBConnection;


namespace AutoCodeCSharp.Controllers
{
    public class AutoCodeController : Controller
    {                       
        // GET: AutoCode
        public ActionResult Index()
        {
            return View();
        }
               

        [ValidateInput(false)]
        public ActionResult List(AutoListModels Models)
        {
            StringBuilder sbREP = new StringBuilder();
            List<string> list1 = new List<string>();
            List<string> list2 = new List<string>();
            List<string> list3 = new List<string>();
            List<string> list4 = new List<string>();
            List<string> list5 = new List<string>();
            List<string> list6 = new List<string>();


            if (Models.List1 != null)
            {
                list1 = Models.List1.Split(',').ToList();
            }

            if (Models.List2 != null)
            {
                list2 = Models.List2.Split(',').ToList();
            }

            if (Models.List3 != null)
            {
                list3 = Models.List3.Split(',').ToList();

            }


            if (Models.List4 != null)
            {
                list4 = Models.List4.Split(',').ToList();

            }


            if (Models.List5 != null)
            {
                list5 = Models.List5.Split(',').ToList();

            }


            if (Models.List6 != null)
            {
                list6 = Models.List6.Split(',').ToList();

            }



            if (Models.Code != null)
            {
                for (int i = 0; i < list1.Count; i++)
                {
                    string sLine = string.Empty;
                    sLine = Models.Code.Replace("@P1", list1[i]);

                    if (list2.Count == list1.Count)
                    {
                        sLine = sLine.Replace("@P2", list2[i]);
                    }

                    if (list3.Count == list1.Count)
                    {
                        sLine = sLine.Replace("@P3", list3[i]);
                    }

                    if (list4.Count == list1.Count)
                    {
                        sLine = sLine.Replace("@P4", list4[i]);
                    }

                    if (list5.Count == list1.Count)
                    {
                        sLine = sLine.Replace("@P5", list5[i]);
                    }

                    if (list6.Count == list1.Count)
                    {
                        sLine = sLine.Replace("@P6", list6[i]);
                    }



                    sbREP.Append(sLine);
                    sbREP.Append("\r\n");
                }
                InsertDB(Models.Code);
            }
           
            ViewData["txtREP"] = sbREP.ToString();
            return View();
        }

        [ValidateInput(false)]
        public ActionResult sbCmd(AutoListModels Models)
        {

            StringBuilder sbREP = new StringBuilder();
            if (Models.List1 != null && Models.Code != null)
            {
                List<string> listSQL = Models.Code.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();

                foreach (string sql in listSQL)
                {
                    if (sql.Trim() == string.Empty)
                    {
                        continue;
                    }

                    sbREP.Append(Models.List1 + "(\" ");
                    sbREP.Append(sql);
                    sbREP.Append(" \")");
                    sbREP.Append("\r\n");
                }
                InsertDB(Models.Code);
            }
          
            ViewData["txtREP"] = sbREP.ToString();
            return View();
        }


        [ValidateInput(false)]
        public ActionResult GroupBy(AutoListModels Models)
        {
            string sSlip = "\r\n";
            if (Models.List1 != null)
            {
                sSlip = Models.List1;
            }

            StringBuilder sbREP = new StringBuilder();
            string REP = "";
            if (Models.Code != null &&  Models.Code != string.Empty )
            {
                List<string> listString = Models.Code.Split(new string[] { sSlip }, StringSplitOptions.None).ToList();

                var pl = from r in listString
                         group r by r into grp
                         select new { key = grp.Key, cnt = grp.Count() } into selection
                         orderby selection.cnt descending, selection.key
                         select selection;


                 var aa1 = pl.ToList();

                 REP = string.Join("\r\n", pl.Select(x => "CNT = " + x.cnt .ToString ().PadLeft (3,'0')+ "      " + x.key.ToString()));


               
                InsertDB(Models.Code);
            }

            ViewData["txtREP"] = REP;
            return View();
        }



        protected void InsertDB(string sCode)
        {
            if (sCode.Length > 0)
            {
                if (sCode.Length > 500)
                {
                    sCode = sCode.Substring(0, 500);
                }


                DBTransaction dbClass = new DBTransaction();
                dbClass.OpenConnection();

                StringBuilder sbCmd = new StringBuilder();
                sbCmd.Append("INSERT INTO TRN_HIS_CODE(BASE_CODE,CRT_DT) VALUES(@BASE_CODE,@CRT_DT)");

                List<ParamClass> listParam = new List<ParamClass>();

                listParam.Add(new ParamClass("@BASE_CODE", sCode));
                listParam.Add(new ParamClass("@CRT_DT", DateTime.UtcNow.AddHours(9)));
                int iExec = dbClass.GetExecuteNum(sbCmd.ToString(), listParam);
            }


        }

    }
}